// *******************************************************************************************************
// # INQUIRY
//   / etc / modal
// *******************************************************************************************************
this.rjapmn_etc = this.rjapmn_etc || {};

/*
▼プルダウンで都道府県のvalue保持＆JSON読み込み
rjapmn_etc.initBtnSelectPrefArea()でデータ取得　行数111行目あたり
rjapmn_etc.onLoadJson()で、上記の値を元に任意のJSONを読み込み　行数123行目あたり


▼プルダウンで店舗名のvalue保持
rjapmn_etc.createBtnSelectShop() 行数156行目あたり
一時、配列rjapmn_etc.data.selectArrayにそのデータを保持

▼確定ボタンで、上記店舗名のデータを取得&使用してください。
rjapmn_etc.initBtnEntry() 行数81行目あたり

*/

(function(){
	rjapmn_etc.data = {};

	//ie6,7判定
	rjapmn_etc.data.ie6 = (uaInfo.isIe && uaInfo.ieVr <= 6) ? true : false;
	rjapmn_etc.data.ie7 = (uaInfo.isIe && uaInfo.ieVr <= 7) ? true : false;

	//json（都道府県選択後、、任意の店舗用JSONへのパスを代入）
	rjapmn_etc.data.PATH_JSON = "";

	//最終的に選択されたショップ名（value）を格納
	rjapmn_etc.data.selectArray = [];

	//データの初期化時に必要なflag
	rjapmn_etc.data.is_first = true;

	//----------------------------------------------------------------------------
	// INIT - Event
	//----------------------------------------------------------------------------
	rjapmn_etc.init = function(){

		modalAreaAddOrDeleteSelectShop.hide();
		$("#modal_pulldown_area").css({opacity:1, marginTop:'0'});

		//都道府県用のプルダウンのセレクト設定
		//このプルダウンからの都道府県選択で、任意のjsonを読み込みます。
		rjapmn_etc.initBtnSelectPrefArea();

		//確定ボタン
		rjapmn_etc.initBtnEntry();
	}

	//----------------------------------------------------------------------------
	// INIT - Dataクリア　モーダル起動の際に初期化
	//----------------------------------------------------------------------------
	rjapmn_etc.clearData = function(){

		//このflagを参考に、店舗名のプルダウンリストの出しわけをします。
		rjapmn_etc.data.is_first = false;

		rjapmn_etc.data.selectArray = [];

		$("#modal_pulldown_area .pref_area select option:selected").attr("selected",false)
		$("#modal_pulldown_area .pref_area select option:eq(0)").attr('selected', true);

		//プルダウンicheckの初期表示ボタンの文言を初期化
		$("#modal_pulldown_area .pref_area .custom_selectInner").html("都道府県");

		//引数用ダミーのobjectです。特に意味はありません。
		var _data = {};
		rjapmn_etc.createBtnSelectShopArea(_data);

		//全てのデータ初期化後、元に戻します
		rjapmn_etc.data.is_first = true;
	}

	//----------------------------------------------------------------------------
	// INIT - Btn 確定
	//----------------------------------------------------------------------------
	rjapmn_etc.initBtnEntry = function(){
		$('#modal_pulldown_area .btn_entry').on('click', function(){

			if(rjapmn_etc.data.selectArray.length>0){
				
				//rjapmn_etc.data.selectArrayに選択された店舗名を保持しています。

				//▼※
				//店舗名(value) = rjapmn_etc.data.selectArray[0];


				//inputエリアに店舗名を代入
				rjapmn_etc.setInputValueShopName();

				//モーダルHIDE
				modalAreaAddOrDeleteSelectShop.hide();
			}
		});
	}

	//inputエリアに店舗名を代入
	rjapmn_etc.setInputValueShopName = function(){
		$('#shopname_value').addClass("input-focus");
		//店舗名(見た目表示用にinputに代入する店名) = rjapmn_etc.data.selectArray[1];
		document.getElementById('shopname_value').value=rjapmn_etc.data.selectArray[1];
	}

	//----------------------------------------------------------------------------
	// INIT - 都道府県用プルダウン
	//----------------------------------------------------------------------------
	rjapmn_etc.initBtnSelectPrefArea = function(){
		$('#modal_pulldown_area .pref_area li select').on('change', function(e){
			var _value = e.target.value;

			if(_value != "all"){
				//バリューによって任意のJSON読み込み
				rjapmn_etc.onLoadJson(_value);

			}
		});
	}

	rjapmn_etc.onLoadJson = function(value){

		//引数のvalueには、option valueの、"hokkaido"などが入ってきます。
		//その値から、任意のjsonを読み込んでください。
//		rjapmn_etc.data.PATH_JSON = "/inquiry/etc/modal/json/modal.json";
		rjapmn_etc.data.PATH_JSON = "/shopajax/";

		rjapmn_etc.loadDataUtil.onGet("json", rjapmn_etc.data.PATH_JSON, rjapmn_etc, "onLoadDataUtilComplete");
	}

	//----------------------------------------------------------------------------
	// LOAD - JSON
	//----------------------------------------------------------------------------
	rjapmn_etc.onLoadDataUtilComplete = function(data){
		//console.log("data="+data.length);

		rjapmn_etc.createBtnSelectShopArea(data);
	}

	//----------------------------------------------------------------------------
	// EVENT - <li>エリア制作
	//----------------------------------------------------------------------------
	rjapmn_etc.createBtnSelectShopArea = function(data){
		$('#modal_pulldown_area .shop_area li.shop').remove();

		//大枠となる<li>部分を作成
		$('#modal_pulldown_area .shop_area').prepend('<li class="shop"></li>');

		rjapmn_etc.createBtnSelectShop(data);
	}

	//----------------------------------------------------------------------------
	// EVENT - <select>エリア制作
	//----------------------------------------------------------------------------
	rjapmn_etc.createBtnSelectShop = function(data){

		var htmlSouce = "";
		htmlSouce = rjapmn_etc.getHtmlSelectSource(data);

		$('#modal_pulldown_area .shop_area li').prepend(htmlSouce);

		if(!rjapmn_etc.data.ie7){
			//IE7以下には、スタイルを設定しない
			$('#modal_pulldown_area .shop_area li select').customSelect({customClass: 'custom_select'});
		}

		$('#modal_pulldown_area .shop_area li select').on('change', function(e){
			var _value = e.target.value;
			var _text = $('#modal_pulldown_area .shop_area li select option:selected').text();

			//選択された店舗名（value）、後にinputに代入する店舗名（漢字）を一時、配列に保持
			if(_value != "all"){
				rjapmn_etc.data.selectArray = [];
				rjapmn_etc.data.selectArray[0] = _value;
				rjapmn_etc.data.selectArray[1] = _text;
			}
		});
	}

	//----------------------------------------------------------------------------
	// EVENT - <select><option>タグを生成
	//----------------------------------------------------------------------------
	rjapmn_etc.getHtmlSelectSource = function(data){
		var _length=data.length;
		var _source = "";
		

		_source +='<select name="shop" id="">';
		
		//JSON読み込み後のoption制作用
		if(rjapmn_etc.data.is_first){

			_source +='<option value="all" selected>店舗を選択してください</option>';

			for(var i=0; i<_length; i++){
				_source += '<option value="'+ data[i]["value"] +'">'+ data[i]["name"] +'</option>';
			}

		//モーダルを表示の際の初期化用
		}else{
			_source +='<option value="all" selected>店舗を選択してください</option>';
		}

		_source +='</select>';

		return _source;
	}

	//----------------------------------------------------------------------------
	// LOAD Data
	//----------------------------------------------------------------------------
	rjapmn_etc.loadDataUtil = {
		_scope:[],
		_callback:[],

		onGet:function(sDataType, sUrl, oScope, sCallback){
			var _obj = {};
			if(oScope)this._scope[0] = oScope;
			if(sCallback)this._callback[0] = sCallback;
			//var nocacheUrl = sUrl+"?"+parseInt((new Date)/1000);
			_obj = {
				type:"GET",
				dataType:'text',
				url:sUrl +'?prefcd=' + $('select[name=pref]').val(),
				cache:false,
				timeout:2000,
				success:rjapmn_etc.loadDataUtil.onSuccess,
				error:rjapmn_etc.loadDataUtil.onError,
				complete:rjapmn_etc.loadDataUtil.onComplete
			}
			jQuery.ajax(_obj);
		},
		onSuccess: function(data, status){
			if(status != 'success')return;

			var vals = data.split(';')
			_source ='<select name="shop" id="">';
			
			//JSON読み込み後のoption制作用
			if(vals.length > 0){

				_source +='<option value="all" selected>店舗を選択してください</option>';

				for(var i=0; i<vals.length; i++){
					_source += '<option value="'+ vals[i] +'">'+ vals[i] +'</option>';
				}

			//モーダルを表示の際の初期化用
			}else{
				_source +='<option value="all" selected>店舗を選択してください</option>';
			}

			_source +='</select>';

			$('#modal_pulldown_area .shop_area li').empty();
			$('#modal_pulldown_area .shop_area li').append(_source);

			if(!rjapmn_etc.data.ie7){
				//IE7以下には、スタイルを設定しない
				$('#modal_pulldown_area .shop_area li select').customSelect({customClass: 'custom_select'});
			}

			$('#modal_pulldown_area .shop_area li select').on('change', function(e){
				var _value = e.target.value;
				var _text = $('#modal_pulldown_area .shop_area li select option:selected').text();

				//選択された店舗名（value）、後にinputに代入する店舗名（漢字）を一時、配列に保持
				if(_value != "all"){
					rjapmn_etc.data.selectArray = [];
					rjapmn_etc.data.selectArray[0] = _value;
					rjapmn_etc.data.selectArray[1] = _text;
				}
			});
			
			//alert(data);
			//(function(){rjapmn_etc.loadDataUtil._scope[0][rjapmn_etc.loadDataUtil._callback[0]](data)})();
		},
		onError:function(XMLHttpRequest, textStatus, errorThrown){
			console.log("LoadData-Error/textStatus="+errorThrown);
		},
		onComplete:function(XMLHttpRequest, textStatus){
		}
	}

	//----------------------------------------------------------------------------
	// MODAL
	//----------------------------------------------------------------------------
	rjapmn_etc.createShopSelectModal = function(){
		$('html,body').stop().animate({ scrollTop: 0 }, 1 );
		modalAreaAddOrDeleteSelectShop.alpha(0.8);
		modalAreaAddOrDeleteSelectShop.show();
	}

	// 追加・削除
	var modalAreaAddOrDeleteSelectShop = (function(){
		var that = {
			modal: null,
			defInput: []
		};
		function init(){
			that.modal = new APMN.Modal({
				target: $('#modal_pulldown_area'),
				close:  $('#modal_pulldown_area .modal_close'),
				ieVr: !(rjapmn_etc.data.ie6)
			});

			$("#modal_pulldown_area .modal_close a img") .hover(function(){
				$(this).css('opacity', '0.7');
			},function(){
				$(this).css('opacity', '1');
			});

			$('#modal_pulldown_area .modal_close a').on('click', function(e){
				e.preventDefault();
			});
		};

		that.show = function(){
			rjapmn_etc.clearData();
			that.modal.show();
		};
		
		that.hide = function(){
			that.modal.hide();
		};

		that.alpha = function(num){
			that.modal.alpha(num);
		};

		init();
		return that;
	}());
})();

//----------------------------------------------------------------------------
// INIT
//----------------------------------------------------------------------------
$(document).ready(
	function(){
		rjapmn_etc.init();
	}
);
